_NOWPAGE_NEW = 1;
_ISOVER = false;

$(function () {
    // 获取首页信息
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    // 获取最新列表
    var result = Model.goods.getNewGoods({
        index: 1,
        size: _NOWPAGE_NEW * 50
    });
    if (!sessionStorage.getItem('hmdj_stopError')) {
        sessionStorage.setItem('hmdj_stopError', true);
    } else {
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
    }
    for (var i = 0; i < result.data.result.length; i++) {
        var item = '';
        if (result.data.result[i].businessId == null) {
			item = '<a class="goods-flow-item" href="views/goods_detail.html?gid=' + result.data.result[i].id + '"><img class="goods-flow-item-thumb lazy-img" data-original="' + result.data.result[i].picture + '"><img class="goods-flow-item-portrait lazy-img" data-original="' + result.data.result[i].photo + '"><div class="goods-flow-item-price">降拍价￥1 - ￥<span class="item-price-title">' + result.data.result[i].topPrice + '</span></div></a>'
		} else {
			item = '<a class="goods-flow-item" href="views/goods_detail.html?gid=' + result.data.result[i].id + '&bid=' + result.data.result[i].businessId + '"><img class="goods-flow-item-thumb lazy-img" data-original="' + result.data.result[i].picture + '"><img class="goods-flow-item-portrait lazy-img" data-original="' + result.data.result[i].photo + '"><div class="goods-flow-item-price">降拍价￥1 - ￥<span class="item-price-title">' + result.data.result[i].topPrice + '</span></div></a>'
		}
        $('.goods-flow-list').append(item);
    }
    // 有信息
    if (result.data.order_count > 0) {
		var item = '<div class="count-icon">' + result.data.order_count + '</div>'
		$('.weui-tabbar__item').eq(3).append(item);
	}
    // 图片懒加载
    lazyImg();
    loading.hide();
});

// 图片懒加载
function lazyImg() {
    $(".lazy-img").lazyload({
        // effect: "fadeIn",
        placeholder: "img/grey.gif",
        threshold: 300
    });
}

// 推荐按钮
$(document).on('click', '.commend-btn', function () {
    $('.load-more-btn').css('display', 'none');
    $('.weui-navbar__item').removeClass('weui-bar__item_on');
    $(this).addClass('weui-bar__item_on');
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    $('.goods-flow-list').empty();
    // 获取推荐列表
    var result = Model.goods.getCommendGoods();
    if (result.code) {
        Common.fn.formatOutput(result);
        loading.hide();
        return false;
    }
    for (var i = 0; i < result.data.length; i++) {
        var item = '<a class="goods-flow-item" href="views/goods_detail.html?gid=' + result.data[i].goodsId + '"><img class="goods-flow-item-thumb lazy-img" data-original="' + result.data[i].picture + '"><img class="goods-flow-item-portrait lazy-img" data-original="' + result.data[i].photo + '"><div class="goods-flow-item-price">降拍价￥1 - ￥<span class="item-price-title">' + result.data[i].topPrice + '</span></div></a>'
        $('.goods-flow-list').append(item);
    }
    // 图片懒加载
    lazyImg();
    loading.hide();
});

// 最新按钮
$(document).on('click', '.new-btn', function () {
    $('.load-more-btn').css('display', 'block');
    $('.weui-navbar__item').removeClass('weui-bar__item_on');
    $(this).addClass('weui-bar__item_on');
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    $('.goods-flow-list').empty();
    // 获取最新列表
    var result = Model.goods.getNewGoods({
        index: 1,
        size: _NOWPAGE_NEW * 50
    });
    if (result.code) {
        Common.fn.formatOutput(result);
        loading.hide();
        return false;
    }
    for (var i = 0; i < result.data.result.length; i++) {
		var item = ''
		if (result.data.result[i].businessId == null) {
			item = '<a class="goods-flow-item" href="views/goods_detail.html?gid=' + result.data.result[i].id + '"><img class="goods-flow-item-thumb lazy-img" data-original="' + result.data.result[i].picture + '"><img class="goods-flow-item-portrait lazy-img" data-original="' + result.data.result[i].photo + '"><div class="goods-flow-item-price">降拍价￥1 - ￥<span class="item-price-title">' + result.data.result[i].topPrice + '</span></div></a>'
		} else {
			item = '<a class="goods-flow-item" href="views/goods_detail.html?gid=' + result.data.result[i].id + '&bid=' + result.data.result[i].businessId + '"><img class="goods-flow-item-thumb lazy-img" data-original="' + result.data.result[i].picture + '"><img class="goods-flow-item-portrait lazy-img" data-original="' + result.data.result[i].photo + '"><div class="goods-flow-item-price">降拍价￥1 - ￥<span class="item-price-title">' + result.data.result[i].topPrice + '</span></div></a>'
		}
		$('.goods-flow-list').append(item)
	}
    // 图片懒加载
    lazyImg();
    loading.hide();
});

// 监听滚轴
window.onscroll = function () {
    var contentHeight = parseInt($('.goods-flow-list').css('height')),
        scrollTop = $(window).scrollTop(),
        isOver = !!sessionStorage.getItem('hmdj_index_over');
    if(contentHeight - 588 - 500 <= scrollTop && !isOver) {
        loadingMore();
    }
}

// 点击加载更多
function loadingMore() {
    console.log('当前滚轮高度：' + $(window).scrollTop());
    console.log('当前div高度：' + $('.goods-flow-list').css('height'));
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    _NOWPAGE_NEW++;
    var result = Model.goods.getNewGoods({
        index: _NOWPAGE_NEW
    });
    if (result.data.result.length == 0) {
		// 没有更多的数据
		_ISOVER = true
		$('.load-more-btn').text('没有更多了')
		loading.hide()
		return false
	}
    for (var i = 0; i < result.data.result.length; i++) {
        var item = '';
        if (result.data.result[i].businessId == null) {
			item = '<a class="goods-flow-item" href="views/goods_detail.html?gid=' + result.data.result[i].id + '"><img class="goods-flow-item-thumb lazy-img" data-original="' + result.data.result[i].picture + '"><img class="goods-flow-item-portrait lazy-img" data-original="' + result.data.result[i].photo + '"><div class="goods-flow-item-price">降拍价￥1 - ￥<span class="item-price-title">' + result.data.result[i].topPrice + '</span></div></a>'
		} else {
			item = '<a class="goods-flow-item" href="views/goods_detail.html?gid=' + result.data.result[i].id + '&bid=' + result.data.result[i].businessId + '"><img class="goods-flow-item-thumb lazy-img" data-original="' + result.data.result[i].picture + '"><img class="goods-flow-item-portrait lazy-img" data-original="' + result.data.result[i].photo + '"><div class="goods-flow-item-price">降拍价￥1 - ￥<span class="item-price-title">' + result.data.result[i].topPrice + '</span></div></a>'
		}
        $('.goods-flow-list').append(item);
    }
    // 图片懒加载
    lazyImg();
    loading.hide();
};